<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mr_tapel extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->isLogin();
		$this->num_post=10;		
		$this->load->model("master/master_tapel"); 		
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index($offset = 0){
		
		$data['main_view']='master/mr_tapel/read';
		$data['aksi_cari']='admin_client/cari_client';
		$uri_segment = 3;
		$num_post=$this->num_post;
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		
		$data['hasil']=$this->master_tapel->read($num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_tapel->count_all_num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'mr_tapel/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			
			$config['prev_link'] = '<<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			
			$config['last_link'] = '>';
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			
			$config['first_link'] = '<';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			
			$config['next_link'] = '>>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('index',$data);
	}
	
	function add()
	{
		$data['main_view']='master/mr_tapel/add';
		$this->load->view('index',$data);
	}
	
	function add_exe()
	{
		$tahun1=$this->input->post('tahun1');
		$tahun2=$this->input->post('tahun2');
		$tapel=$tahun1."/".$tahun2;
		$periode_awal=$tahun1."-7";
		$insert=$this->master_tapel->insert($tapel);
		if($insert){
			$id_tapel=$this->db->insert_id();
			for($i=0;$i<12;$i++){
				$periode_berikut= strtotime ( '+'.$i.' month' , strtotime ( $periode_awal ) ) ; 
				$str_periode=date('Y-m',$periode_berikut);
				//echo $str_berikut."<br>";
				$urut=$i+1;
				$ins_periode=$this->master_tapel->insert_periode($id_tapel,$str_periode,$urut);
			}
		}
				
		redirect ('mr_tapel');
	}	
	
	
	
	
	function set_aktif($id,$status){
		if($query=$this->master_tapel->set_aktif($id,$status)){
			redirect('mr_tapel');
		}
	}
	
	function delete(){
		$id = $this->security->xss_clean($this->uri->segment(3));
		$this->master_tapel->delete($id);
		redirect('mr_tapel');
	}
	
}